package cn.getech.data.development.controller;


import avro.shaded.com.google.common.collect.Lists;
import avro.shaded.com.google.common.collect.Maps;
import cn.getech.data.development.config.properties.BdpJobConfig;
import cn.getech.data.development.constant.*;
import cn.getech.data.development.dto.EsDto;
import cn.getech.data.development.dto.JobNodeConfigDto;
import cn.getech.data.development.entity.*;
import cn.getech.data.development.entity.permission.SysRoleDataPermission;
import cn.getech.data.development.entity.permission.SysTenantDataPermission;
import cn.getech.data.development.entity.vo.ConfConnectDetail;
import cn.getech.data.development.model.dto.ConfConnectDto;
import cn.getech.data.development.model.dto.DataSyncDataSourceDTO;
import cn.getech.data.development.model.qo.ConfConnectQO;
import cn.getech.data.development.model.req.confconnect.TableInfoFieldRelationReq;
import cn.getech.data.development.model.req.confconnect.TableInfoFieldReq;
import cn.getech.data.development.model.res.confconnect.TableInfoFieldRelationRes;
import cn.getech.data.development.model.res.confconnect.TableInfoFieldRes;
import cn.getech.data.development.model.resp.ConfConnectRespVO;
import cn.getech.data.development.model.vo.*;
import cn.getech.data.development.service.*;
import cn.getech.data.development.utils.DataPermissionUtil;
import cn.getech.data.development.utils.HdfsUtil;
import cn.getech.data.development.utils.SecurityUtil;
import cn.getech.data.development.utils.sap.SapCheckUtils;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.BaseRes;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.PojoUtils;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.data.intelligence.common.validator.ValidatorUtils;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.config.properties.SystemCenterConfig;
import cn.getech.system.center.constant.DefConnectEnum;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.getech.system.center.model.dto.SysUserDto;
import cn.getech.system.center.service.IDepartmentService;
import cn.getech.system.center.service.SysUserService;
import cn.getech.system.center.utils.AES;
import cn.getech.system.center.utils.ShiroUtils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Sets;
import com.xxl.job.core.log.XxlJobLogger;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 配置连接
 *
 * @author zenith
 * @Date 2018-08-22 09:54:23
 */
@Slf4j
@Api(tags = "配置连接")
@RestController
@RequestMapping("/bdp/confConnect")
public class ConfConnectController {


    @Autowired
    private ConfConnectService confConnectService;

    @Autowired
    private ProcConnectService procConnectService;

    @Autowired
    private JobInfoService jobInfoService;

    @Autowired
    private JobNodeInfoService jobNodeInfoService;
    @Autowired
    private JobNodeConfService jobNodeConfService;

    @Autowired
    private ConnectFieldInfoService connectFieldInfoService;
    @Autowired
    private SapConnectFieldService sapConnectFieldService;
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private BdpJobConfig bdpJobConfig;
    @Autowired
    private DataPermissionService dataPermissionService;
    @Autowired
    private EsService esService;
    @Autowired
    private SystemCenterConfig systemCenterConfig;
    @Autowired
    private DataSyncInfoService dataSyncInfoService;
    @Autowired
    private IDepartmentService iDepartmentService;
    @Autowired
    private ProcUserService procUserService;

    /*
    对数据源配置表中所有未进行加密的密码进行加密
     */
    @GetMapping("/saveAnd")
    public R saveAnd() {
        List<ConfConnect> confConnects = confConnectService.list();
        log.info("共" + confConnects.size());
        confConnects.forEach(confConnect -> {
            if (null != confConnect.getPassword() && !"".equals(confConnect.getPassword())) {
                try {
                    securityUtil.decrypt(confConnect.getPassword());
                    log.info("无需加密--" + confConnect.getId());
                } catch (Exception e) {
                    try {
                        confConnect.setPassword(securityUtil.encrypt(confConnect.getPassword()));
                    } catch (Exception ex) {
                        log.info("加密失败" + confConnect.getId());
                    }
                    confConnectService.updateById(confConnect);
                    log.info("加密成功--" + confConnect.getId());
                }
            }
        });
        return R.ok();
    }

    /*
    hdfs上所有有关数据接入和数据推送的json文件，对文件中未加密的密码进行加密
     */
    @GetMapping("/saveJson")
    public R saveJson(HttpServletResponse response) {
        List<JobNodeInfo> jobNodeInfos = jobNodeInfoService.list(new QueryWrapper<JobNodeInfo>().eq("type_id", 4).or().eq("type_id", 1));
        log.info("共计" + jobNodeInfos.size());
        jobNodeInfos.forEach(param -> {
            String name = param.getId().toString();
            log.info(name + ":");
            HdfsUtil hdfsUtil = null;
            try {
                hdfsUtil = new HdfsUtil(bdpJobConfig);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (null != hdfsUtil) {
                try {
                    String str = hdfsUtil.readFile2(String.format("%s/%s/%s%s.json", bdpJobConfig.getJobconfig(), param.getTypeId(), "jobid_", name));
                    JSONObject jsonObject = JSONObject.parseObject(str);
                    saveJsonPassword(jsonObject, param, hdfsUtil);
                } catch (RRException r) {
                    log.info("不存在--" + String.format("%s/%s/%s%s.json", bdpJobConfig.getJobconfig(), param.getTypeId(), "jobid_", name));
                } catch (IOException i) {
                    log.info("IO出错--" + String.format("%s/%s/%s%s.json", bdpJobConfig.getJobconfig(), param.getTypeId(), "jobid_", name));
                } catch (Exception e) {
                    log.info("加密失败--" + String.format("%s/%s/%s%s.json", bdpJobConfig.getJobconfig(), param.getTypeId(), "jobid_", name));
                }
            }
        });
        return R.ok();
    }

    public void saveJsonPassword(JSONObject jsonObject, JobNodeInfo param, HdfsUtil hdfsUtil) throws Exception {
        String name = "";
        if (null != jsonObject.get("password") && !"".equals(jsonObject.get("password"))) {
            name = "password";
        } else if (null != jsonObject.get("ftp_passwd") && !"".equals(jsonObject.get("ftp_passwd"))) {
            name = "ftp_passwd";
        } else if (null != jsonObject.get("passwd") && !"".equals(jsonObject.get("passwd"))) {
            name = "passwd";
        } else if (null != jsonObject.get("jCO_PASSWD") && !"".equals(jsonObject.get("jCO_PASSWD"))) {
            name = "jCO_PASSWD";
        } else {
            log.info("无密码--" + String.format("%s/%s/%s%s.json", bdpJobConfig.getJobconfig(), param.getTypeId(), "jobid_", param.getId()));
        }
        if (!"".equals(name)) {
            try {
                securityUtil.decrypt(jsonObject.getString(name));
                log.info("无需加密--" + String.format("%s/%s/%s%s.json", bdpJobConfig.getJobconfig(), param.getTypeId(), "jobid_", param.getId()));
            } catch (Exception e) {
                log.info(String.format("%s/%s/%s%s.json", bdpJobConfig.getJobconfig(), param.getTypeId(), "jobid_", param.getId()) + ":" + jsonObject.get(name) + "进行加密：");
                jsonObject.put(name, securityUtil.encrypt(jsonObject.getString(name)));
                try {
                    hdfsUtil.writeFile(jsonObject.toJSONString().getBytes(), String.format("%s/%s/%s%s.json", bdpJobConfig.getJobconfig(), param.getTypeId(), "jobid_", param.getId()));
                    hdfsUtil.close();
                } catch (Exception e1) {
                    if (null != hdfsUtil) {
                        hdfsUtil.close();
                    }
                }
            }
        }
    }


    /**
     * 获取已关联的配置连接列表
     */
    @ApiOperation("name:搜索,proc_id:工程内数据源卡片页，jobId和type_id:任务内节点数据源")
    //@SysLog(value="获取已关联的配置连接列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @PostMapping("/list")
    //todo 0925
    //@RequiresPermissions(value = {"bdp:procInfo:config"})
    public R list(@ApiParam(value = "{'name':'test','proc_id':46,'jobId':1,'type_id':1,'searchUserId':318}") @RequestBody Map<String, Object> params) {
        if (null == params.get("proc_id")) {
            if (null == params.get("jobId")) {
                throw new RRException(String.format(DataDevelopmentBizExceptionEnum.PARAM_FAIL_ERROR.getMessage(), "工程id或者任务id不能为空"));
            }
            //通过job_id查询到对应的job信息
            JobInfo jobInfo = jobInfoService.getById((Serializable) params.get("jobId"));
            if (null == jobInfo) {
                throw new RRException(String.format(DataDevelopmentBizExceptionEnum.PARAM_FAIL_ERROR.getMessage(), "工程id或者任务id不能为空"));
            }
            params.put("proc_id", jobInfo.getProcId());
        }
//        List<ProcConnect> procConnectList = procConnectService.list(new QueryWrapper<ProcConnect>()
//                .eq("proc_id",params.get("proc_id"))
//                .like(params.get("name")!=null&&StringUtil.isNotEmpty(params.get("name").toString()),"name",params.get("name")));
//        for(ProcConnect p : procConnectList){
//            Integer confConnectId = p.getConnectId();
//            ConfConnect confConnect = confConnectService.getById(confConnectId);
        List<ConfConnect> dataSourceList = new ArrayList<>();
        if (params.containsKey("page") && params.containsKey("limit")) {
            PageUtils conConnectPage = confConnectService.pageByNameAndProId(params);
            List<ConfConnect> confConnectList = (List<ConfConnect>) conConnectPage.getList();
            handleConfConnect(dataSourceList, confConnectList, params);
            conConnectPage.setList(dataSourceList);
            return R.okWithPage(conConnectPage);
        } else {
            List<ConfConnect> confConnectList = confConnectService.selectByNameAndProId(params);
            handleConfConnect(dataSourceList, confConnectList, params);
            return R.okWithData(dataSourceList);
        }
    }

    private void handleConfConnect(List<ConfConnect> dataSourceList, List<ConfConnect> confConnectList, Map<String, Object> params) {
        for (ConfConnect confConnect : confConnectList) {
            if (params.get("type_id") == null || ObjectUtils.equals(confConnect.getTypeId(), Integer.valueOf(params.get("type_id").toString()))) {
                //针对sap数据源获取sap数据源配置信息checkSapPartition
                if (null != confConnect) {
                    if (confConnect.getTypeId().equals(ConnectTypeEnum.Sap.getCode())) {
                        SapConnectFieldDb sapConnectFieldDb = sapConnectFieldService.getOne(new QueryWrapper<SapConnectFieldDb>().eq("connect_id", confConnect.getId()));
                        if (sapConnectFieldDb != null) {
                            String inputParams = sapConnectFieldDb.getAinputParams();
                            String outputParams = sapConnectFieldDb.getAoutputParams();
                            SapConnectField sapConnectField = new SapConnectField();
                            BeanUtils.copyProperties(sapConnectFieldDb, sapConnectField);
                            sapConnectField.setAinputParams(JSONObject.parseArray(inputParams, JSONObject.class));
                            sapConnectField.setAoutputParams(JSONObject.parseArray(outputParams, JSONObject.class));
                            confConnect.setSapConnectFieldInfo(sapConnectField);
                        }
                    }
                    confConnect.setTypeName(ConnectTypeEnum.ObjOf(confConnect.getTypeId()).getName());
                }
            }
            dataSourceList.add(confConnect);
        }
        filetInfo2(confConnectList);
    }

    private void filetInfo(List<ConfConnect> confConnectList) {
        List<SysUserDto> userList = sysUserService.listAllUsers();
        Map<Long, String> userMap = userList.stream().collect(
                Collectors.toMap(SysUserDto::getUserId, SysUserDto::getUsername));
        for (ConfConnect confConnect : confConnectList) {
            String info = null;
            if (ConnectTypeEnum.Kafka.getCode().equals(confConnect.getTypeId())) {
                info = String.format("%s | %s", confConnect.getKafkaUrl(), confConnect.getTopic());
            } else if (ConnectTypeEnum.FTP.getCode().equals(confConnect.getTypeId())) {
                info = String.format("%s:%s", confConnect.getHost(), confConnect.getPort());
            } else if (ConnectTypeEnum.Sap.getCode().equals(confConnect.getTypeId())) {
                info = String.format("%s:%s", confConnect.getHost(), confConnect.getPort());
            } else if (ConnectTypeEnum.JieKou.getCode().equals(confConnect.getTypeId())) {
                info = String.format("%s", confConnect.getInterfaceUrl());
            } else if (ConnectTypeEnum.WebService.getCode().equals(confConnect.getTypeId())) {
                if (Objects.equals(3, confConnect.getRequestType())) {
                    info = String.format("%s | %s", confConnect.getWebServiceUrl(), "post-raw");
                } else {
                    info = String.format("%s | %s", confConnect.getWebServiceUrl(), confConnect.getFunctionName());
                }
            } else if (ConnectTypeEnum.OracleNTS.getCode().equals(confConnect.getTypeId())) {
                info = String.format("%s", confConnect.getOracleNtsUrl());
            } else {
                if (StringUtils.isEmpty(confConnect.getDbname()))
                    info = String.format("%s:%s", confConnect.getHost(), confConnect.getPort());
                else
                    info = String.format("%s:%s | %s", confConnect.getHost(), confConnect.getPort(), confConnect.getDbname());
            }
            confConnect.setInfo(info);
            if (confConnect.getCreatePer() != null) {
                Long userId = Long.valueOf(confConnect.getCreatePer());
                confConnect.setCreatePerName(userMap.get(userId));
            }
        }
    }

    private void filetInfo2(List<ConfConnect> confConnectList) {
        List<SysUserDto> userList = sysUserService.listAllUsers();
        Map<Long, String> userMap = userList.stream().collect(
                Collectors.toMap(SysUserDto::getUserId, SysUserDto::getUsername));
        for (ConfConnect confConnect : confConnectList) {
            String info = null;
            if (ConnectTypeEnum.Kafka.getCode().equals(confConnect.getTypeId())) {
                info = String.format("%s | %s", confConnect.getKafkaUrl(), confConnect.getTopic());
            } else if (ConnectTypeEnum.FTP.getCode().equals(confConnect.getTypeId())) {
                info = String.format("%s:%s", confConnect.getHost(), confConnect.getPort());
            } else if (ConnectTypeEnum.Sap.getCode().equals(confConnect.getTypeId())) {
                info = String.format("%s:%s", confConnect.getHost(), confConnect.getPort());
            } else if (ConnectTypeEnum.JieKou.getCode().equals(confConnect.getTypeId())) {
                info = String.format("%s", confConnect.getInterfaceUrl());
            } else if (ConnectTypeEnum.WebService.getCode().equals(confConnect.getTypeId())) {
                if (Objects.equals(3, confConnect.getRequestType())) {
                    info = String.format("%s | %s", confConnect.getWebServiceUrl(), "post-raw");
                } else {
                    info = String.format("%s | %s", confConnect.getWebServiceUrl(), confConnect.getFunctionName());
                }
            } else if (ConnectTypeEnum.OracleNTS.getCode().equals(confConnect.getTypeId())) {
                info = String.format("%s", confConnect.getOracleNtsUrl());
            } else {
                if (StringUtils.isEmpty(confConnect.getDbname()))
                    info = String.format("%s:%s", confConnect.getHost(), confConnect.getPort());
                else
                    info = String.format("%s:%s | %s", confConnect.getHost(), confConnect.getPort(), confConnect.getDbname());
            }
            confConnect.setInfo(info);
            if (confConnect.getCreatePer() != null) {
                Long userId = Long.valueOf(confConnect.getCreatePer());
                confConnect.setCreatePerName(userMap.get(userId));
            }

            if (confConnect.getModPer() != null) {
                Long userId = Long.valueOf(confConnect.getModPer());
                confConnect.setModPerName(userMap.get(userId));
            }

            StringBuilder builder = new StringBuilder();
            if (StringUtils.isNotEmpty(confConnect.getProcNames())) {
                builder.append("关联的项目:<br/>").append(confConnect.getProcNames()).append("<br/>");
            }
            if (StringUtils.isNotEmpty(confConnect.getTaskNames())) {
                builder.append("关联的元数据采集任务:<br/>").append(confConnect.getTaskNames()).append("<br/>");
            }
            if (StringUtils.isNotEmpty(confConnect.getRuleNames())) {
                builder.append("关联的质量规则:<br/>").append(confConnect.getRuleNames()).append("<br/>");
            }
            if (StringUtils.isNotEmpty(confConnect.getServiceNames())) {
                builder.append("关联的数据服务:<br/>").append(confConnect.getServiceNames()).append("<br/>");
            }

            if (StringUtils.isNotEmpty(confConnect.getSyncTaskNames())) {
                builder.append("关联的整库同步任务:<br/>").append(confConnect.getSyncTaskNames()).append("<br/>");
            }

            confConnect.setAppState(2);//1:使用中 2:未使用
            if (builder.length() > 0) {
                confConnect.setAppState(1);
            }
            confConnect.setRelationContent(builder.toString());
        }
    }


    @ApiOperation("获取指定数据库的表和字段和血缘")
    @PostMapping("/getTableInfoFieldRelation")
    public R getTableInfoFieldRelation(@RequestBody TableInfoFieldRelationReq req) {
        TableInfoFieldRelationRes res = confConnectService.getTableInfoField(req);
        if (CollectionUtil.isEmpty(res.getTableFieldInfos())) {
            return R.error("字段信息为空");
        }
        return R.okWithData(res);
    }

    /**
     * 获取所有的数据类型名称
     */
    @ApiOperation("获取所有的数据类型名称")
    //@SysLog(value="获取所有的数据类型名称",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/listDataKaFormats")
    //todo 0925
    //@RequiresPermissions({"bdp:confConnect:list"})
    public R listDataTypes(@RequestParam Integer typeId) {
        if (Objects.equals(ConnectTypeEnum.JieKou.getCode(), typeId) || Objects.equals(ConnectTypeEnum.Kafka.getCode(), typeId)) {
            return R.okWithData(DataKaFormat.values());
        } else if (Objects.equals(ConnectTypeEnum.WebService.getCode(), typeId)) {
            return R.okWithData(DataWebServiceFormat.values());
        }
        return R.okWithData(DataKaFormat.values());
    }

    /**
     * 获取所有的kafka的数据
     */
    @ApiOperation("获取所有的kafka的数据")
    //@SysLog(value="获取所有的kafka的数据",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/listAllKafka")
    public R listAllKafka(@RequestParam(required = false) Integer procId) {
        List<ConfConnectDto> confConnectList = confConnectService.listAllKafka(procId);
        return R.okWithData(PojoUtils.listConvert(ConfConnectRespVO.class, confConnectList));
    }

    /**
     * 根据类型和工程id查询对应的数据源信息
     *
     * @param confConnectQO 查询对象
     * @author: hujz
     * @date: 2019-08-24
     */
    @ApiOperation(value = "根据类型和工程id查询对应的数据源信息")
    //@SysLog(value="根据类型和工程id查询对应的数据源信息",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @PostMapping("/listByTypeIdAndProcId")
    public R listByCondition(@RequestBody ConfConnectQO confConnectQO) {
        List<ConfConnectDto> confConnectDtos = confConnectService.listByCondition(confConnectQO);
        return R.okWithData(PojoUtils.listConvert(ConfConnectVO.class, confConnectDtos));
    }

    /**
     * @param confConnect
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @ApiOperation("数据推送时获取选中数据库的所有表")
    //@SysLog(value="数据推送时获取选中数据库的所有表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/dataBaseTableList")
    public R dataBaseTableList(@ApiParam(value = "{'id':25}") ConfConnect confConnect) {
        if (null == confConnect || null == confConnect.getId()) {
            throw new RRException(DataDevelopmentBizExceptionEnum.PARAM_ERROR.getMessage());
        }
        ConfConnect confConnect1 = confConnectService.getById(confConnect.getId());
        if (confConnect1 == null || !confConnect1.getTypeId().equals(confConnect.getTypeId())) {
            throw new RRException(DataDevelopmentBizExceptionEnum.DATA_SAVE_ERROR.getMessage());
        }
        JSONArray tableNameArray = confConnectService.getDatabaseNameToTableName(confConnect1);
        return R.okWithData(tableNameArray);
    }


    /**
     * 获取配置连接列表
     */
    @ApiOperation("获取配置连接列表")
    //@SysLog(value="获取配置连接列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @PostMapping("/listAll")
    //todo 0925
    //@RequiresPermissions({"bdp:confConnect:list"})
    public R listAll(@ApiParam(value = "{'name':'test','type_id':1}") @RequestBody Map<String, Object> params) {
        params.put("create_per", ShiroUtils.getUserId().intValue());
        params.put("sidx", "id");
        params.put("order", "desc");
        if (params.get("list") == null) {
            params.put("connectEnable", 1);
        }
        PageUtils page = confConnectService.queryPage(params);
        filetInfo2((List<ConfConnect>) page.getList());
        return R.okWithPage(page);
    }

    /**
     * 获取配置连接列表
     */
    @ApiOperation("获取配置连接列表")
    //@SysLog(value="获取配置连接列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @PostMapping("/listJdbc")
    //@RequiresPermissions({"bdp:confConnect:list"})
    public R listJdbc(@ApiParam(value = "{'name':'test','type_id':1}") @RequestBody Map<String, Object> params) {
        params.put("create_per", ShiroUtils.getUserId().intValue());
        params.put("sidx", "id");
        params.put("order", "desc");
        if (params.get("list") == null) {
            params.put("connectEnable", 1);
        }
        params.put("typeIds", Arrays.asList(1, 2, 3, 4));
        PageUtils page = confConnectService.queryPage(params);
        List<ConfConnect> list = (List<ConfConnect>) page.getList();
        filetInfo2(list);
        ConfConnect hiveConnect = new ConfConnect();
        hiveConnect.setId(0);
        hiveConnect.setName("默认数据源");
        list.add(hiveConnect);
        return R.okWithPage(page);
    }

    @ApiOperation("获取数据源数据权限列表")
    @GetMapping("/getDbNames/{id}")
    public R getDbNames(@PathVariable("id") Integer id) {
        if (id == null) {
            return R.error("id 不能为空");
        }
        List<String> dbNames = new ArrayList<>();
        ConfConnect confConnect = null;
        // -1 其它数据源  0 默认hive  -4 默认impala  -5 默认kafka
        if (id == -1) {
            dbNames = confConnectService.getMetaDbNames(id);
            return R.okWithData(dbNames);
        } else if (id == 0 || id == -4 || id == -5) {//默认的其它数据库
            for (DBTypeEnum value : DBTypeEnum.values()) {
                dbNames.add(value.getName());
            }
            return R.okWithData(dbNames);
        } else {
            confConnect = confConnectService.getById(id);
        }
        if (confConnect == null) {
            return R.error("没有获取到该数据源");
        }
        if (confConnect.getConnectEnable() != null
                && confConnect.getConnectEnable().equals(YesOrNoEnum.NO.getValue())) {
            return R.error("该数据源已被禁用");
        }
        dbNames = confConnectService.getDbnames(confConnect);
        return R.okWithData(dbNames);
    }


    /**
     * 获取元数据连接列表
     */
    @ApiOperation("获取元数据连接列表")
    @PostMapping("/listMetaCon")
    public R listMetaCon(@RequestParam(required = false) String name) {
        List<HashMap<String, Object>> list = Lists.newArrayList();
        list.add(new HashMap<String, Object>() {{
            put("id", DefConnectEnum.HIVE.getCode());
            put("name", DefConnectEnum.HIVE.getName());
        }});
        list.add(new HashMap<String, Object>() {{
            put("id", DefConnectEnum.IMPALA.getCode());
            put("name", DefConnectEnum.IMPALA.getName());
        }});
        list.add(new HashMap<String, Object>() {{
            put("id", DefConnectEnum.KAFKA.getCode());
            put("name", DefConnectEnum.KAFKA.getName());
        }});
        list.add(new HashMap<String, Object>() {{
            put("id", DefConnectEnum.OTHER.getCode());
            put("name", DefConnectEnum.OTHER.getName());
        }});
        //有权限的jdbc数据源
        DataSyncDataSourceDTO dataSyncDataSourceDTO = new DataSyncDataSourceDTO();
        List<Integer> typeIds = new ArrayList<>();
        ConnectTypeEnum[] values = ConnectTypeEnum.values();
        for (ConnectTypeEnum value : values) {
            if (value.getType().getCode() == ConTypeType.JDBC.getCode()) {
                typeIds.add(value.getCode());
            }
        }
        dataSyncDataSourceDTO.setTypeIds(typeIds);
        dataSyncDataSourceDTO.setLimit(2000);
        PageUtils pageUtils = dataSyncInfoService.getDataSourceByType(dataSyncDataSourceDTO);
        if (null != pageUtils) {
            List<ConfConnect> list1 = pageUtils.getList();
            if (CollectionUtil.isNotEmpty(list1)) {
                for (ConfConnect confConnect : list1) {
                    list.add(new HashMap<String, Object>() {{
                        put("id", confConnect.getId());
                        put("name", confConnect.getName());
                    }});
                }
            }
        }
        //list.addAll(confConnectService.listMetaCon(name));
        return R.okWithData(list);
    }

    @ApiOperation("获取数据源数据权限列表")
    @GetMapping("/getMetaDbNames/{id}")
    public R getMetaDbNames(@PathVariable("id") Integer id) {
        if (id == null) {
            return R.error("id 不能为空");
        }
        List<String> dbNames = new ArrayList<>();
        if (id == -1) {
            dbNames = confConnectService.getMetaDbNames(id);
            return R.okWithData(dbNames);
        } else if (id <= 0) {
            for (DBTypeEnum value : DBTypeEnum.values()) {
                dbNames.add(value.getName());
            }
            return R.okWithData(dbNames);
        } else {
            dbNames = confConnectService.getMetaDbNames(id);
        }
        return R.okWithData(dbNames);
    }

    @ApiOperation("获取指定数据库的表和字段")
    @PostMapping("/getTableInfoField")
    //@SysLog(value="获取指定数据库的表和字段",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    public R getTableInfoField(@RequestBody TableInfoFieldReq req) {
        List<TableInfoFieldRes> res = confConnectService.getTableInfoField(req);
        return R.okWithData(res);
    }

    /**
     * 获取配置连接列表
     */
    @ApiOperation("获取数据源数据权限列表")
    //@SysLog(value="获取数据源数据权限列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/listByRole")
    //todo 0925
    //@RequiresPermissions("sys:permission")
    public R listByRole(@ApiParam(value = "{'name':'test','department_id':1,'role_id':0}") @RequestParam Map<String, Object> params) {
        params.put("sidx", "id");
        params.put("order", "desc");
        PageUtils page = confConnectService.listPageByRole(params);
        return R.okWithPage(page);
    }

    /**
     * 获取配置连接列表
     */
    @ApiOperation("获取数据源数据权限列表")
    //@SysLog(value="获取数据源数据权限列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/listByTenant")
    //todo 0925
    //@RequiresPermissions("sys:permission")
    public R listByTenant(@ApiParam(value = "{'name':'test','department_id':1,'tenant_id':0}") @RequestParam Map<String, Object> params) {
        if (params.get("tenant_id") == null) {
            return R.error("请选择租户!");
        }
        params.put("sidx", "id");
        params.put("order", "desc");
        PageUtils page = confConnectService.listPageByTenant(params);
        return R.okWithPage(page);
    }

    @ApiOperation(value = "保存数据源数据权限")
    @SysLog(value = "数据源权限授权", type = OperationTypeEnum.APPROVECODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    @PostMapping("/savePermission")
    //todo 0925
    //@RequiresPermissions("sys:permission")
    public R savePermission(@RequestBody List<SysRoleDataPermission> permissionList) {
//        if (CollectionUtil.isNotEmpty(permissionList)) {
//            List<Integer> dataIds = DataPermissionUtil.getDataIds(ShiroUtils.getUserId().intValue(),
//                    PermissionTypeEnum.RESOURCE.getType(),
//                    PermissionTypeEnum.ASSIGN.getType());
//            List<Integer> saveIds = permissionList.stream().map(i -> i.getDataId()).collect(Collectors.toList());
//            if (!dataIds.containsAll(saveIds)) {
//                return R.error("存在无权限数据，请刷新重试");
//            }
//        }
        dataPermissionService.savePermission(permissionList);
        return R.ok();
    }

    @ApiOperation(value = "保存租户数据源数据权限")
    @SysLog(value = "租户数据源权限授权", type = OperationTypeEnum.APPROVECODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    @PostMapping("/saveTenantPermission")
    //todo 0925
    //@RequiresPermissions("sys:permission")
    public R saveTenantPermission(@RequestBody List<SysTenantDataPermission> permissionList) {
        dataPermissionService.saveTenantPermission(permissionList);
        return R.ok();
    }

    /**
     * 获取配置连接列表
     */
    @ApiOperation("获取配置连接列表")
    //@SysLog(value="获取配置连接列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/selectQuery")
    public R selectQuery() {
        List<ConfConnect> page = confConnectService.selectQuery(ShiroUtils.getUserId().intValue());
        filetInfo(page);
        return R.okWithPage(page);
    }

    /**
     * 获取配置连接列表 用于密级查询
     */
    @ApiOperation("获取配置连接列表2")
    //@SysLog(value="获取配置连接列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/selectSourceOther")
    public R selectSourceOther() {
        List<ConfConnect> page = confConnectService.selectQuery2(ShiroUtils.getUserId().intValue());
        filetInfo(page);
        return R.okWithPage(page);
    }


    /**
     * 获取获取数据源的创建人信息，用于前端筛选
     */
    @ApiOperation("获取获取数据源的创建人信息")
    @GetMapping("/userList")//  /bdp/confConnect/userList
    //@SysLog(value="获取获取数据源的创建人信息",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions({"bdp:confConnect:list"})
    public R userList() {
        Map<String, Object> params = Maps.newHashMap();
        params.put("limit", Integer.MAX_VALUE + "");
        params.put("page", "1");
        PageUtils page = confConnectService.listNoRelation(params);
        List<ConfConnect> list = (List<ConfConnect>) page.getList();
        filetInfo(list);
        List<Map<String, Object>> userList = Lists.newArrayList();
        Set<Integer> userIdSet = Sets.newHashSet();
        for (ConfConnect connect : list) {
            if (connect.getCreatePer() == null
                    || StringUtils.isEmpty(connect.getCreatePerName())) {
                continue;
            }
            if (!userIdSet.contains(connect.getCreatePer())) {
                Map<String, Object> user = Maps.newHashMap();
                user.put("userName", connect.getCreatePerName());
                user.put("userId", connect.getCreatePer());
                userList.add(user);
                userIdSet.add(connect.getCreatePer());
            }
        }
        return R.okWithData(userList);
    }

    /**
     * 获取未关联的配置连接列表
     */
    @ApiOperation("获取未关联的配置连接列表")
    //@SysLog(value="获取未关联的配置连接列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/listNoRelation")
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:config"})
    public R listNoRelation(@ApiParam(value = "{'name':'test','proc_id':1,'type_id':1}") @RequestParam Map<String, Object> params) {
        params.put("create_per", ShiroUtils.getUserId().intValue());
        params.put("sidx", "id");
        params.put("order", "desc");
        PageUtils page = confConnectService.listNoRelation(params);
        for (ConfConnect confConnect : (List<ConfConnect>) page.getList()) {
            if (confConnect != null) {
                confConnect.setTypeName(ConnectTypeEnum.ObjOf(confConnect.getTypeId()).getName());
            }
        }
        filetInfo2((List<ConfConnect>) page.getList());
        return R.okWithPage(page);
    }


    public ConfConnect checkConnect(ConfConnect confConnect) {
        ConfConnect confConnect1 = new ConfConnect();
        switch (confConnect.getTypeId()) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 10:
            case 15:
            case 17:
                JDBCConfConnectVO jdbcConfConnectVO = new JDBCConfConnectVO();
                BeanUtils.copyProperties(confConnect, jdbcConfConnectVO);
                ValidatorUtils.validateEntity(jdbcConfConnectVO);
                BeanUtils.copyProperties(jdbcConfConnectVO, confConnect1);
                break;
            case 18:
                FTPConfConnectVO sftpConfConnectVO = new FTPConfConnectVO();
                BeanUtils.copyProperties(confConnect, sftpConfConnectVO);
                ValidatorUtils.validateEntity(sftpConfConnectVO);
                BeanUtils.copyProperties(sftpConfConnectVO, confConnect1);
                break;
            case 16:
                OracleNtsConfConnectVO oracleNtsConfConnectVO = new OracleNtsConfConnectVO();
                BeanUtils.copyProperties(confConnect, oracleNtsConfConnectVO);
                ValidatorUtils.validateEntity(oracleNtsConfConnectVO);
                BeanUtils.copyProperties(oracleNtsConfConnectVO, confConnect1);
                break;
            case 5:
                FTPConfConnectVO ftpConfConnectVO = new FTPConfConnectVO();
                BeanUtils.copyProperties(confConnect, ftpConfConnectVO);
                ValidatorUtils.validateEntity(ftpConfConnectVO);
                BeanUtils.copyProperties(ftpConfConnectVO, confConnect1);
                break;
            case 6:
                KafkaConfConnectVO kafkaConfConnectVO = new KafkaConfConnectVO();
                BeanUtils.copyProperties(confConnect, kafkaConfConnectVO);
                ValidatorUtils.validateEntity(kafkaConfConnectVO);
                BeanUtils.copyProperties(kafkaConfConnectVO, confConnect1);
                break;

            case 7:
                InterfaceConfConnectVO interfaceConfConnectVO = new InterfaceConfConnectVO();
                BeanUtils.copyProperties(confConnect, interfaceConfConnectVO);
                ValidatorUtils.validateEntity(interfaceConfConnectVO);
                BeanUtils.copyProperties(interfaceConfConnectVO, confConnect1);
                break;
            case 8:
                SapConfConnectVO sapConfConnectVO = new SapConfConnectVO();
                BeanUtils.copyProperties(confConnect, sapConfConnectVO);
                ValidatorUtils.validateEntity(sapConfConnectVO);
                BeanUtils.copyProperties(sapConfConnectVO, confConnect1);
                break;
            case 9:
                WebServiceConfConnectVO webServiceConfConnectVO = new WebServiceConfConnectVO();
                BeanUtils.copyProperties(confConnect, webServiceConfConnectVO);
                Integer requestType = confConnect.getRequestType();
                if (Objects.equals(requestType, 3)) {
                    if (StringUtils.isEmpty(confConnect.getWebServiceUrl())) {
                        throw new RRException("请求地址必须填写！");
                    }
                } else {
                    ValidatorUtils.validateEntity(webServiceConfConnectVO);
                }
                BeanUtils.copyProperties(webServiceConfConnectVO, confConnect1);
                break;
            case 11:
                // hive 连接
                HiveConfConnectVO hiveConfConnectVO = new HiveConfConnectVO();
                BeanUtils.copyProperties(confConnect, hiveConfConnectVO);
                ValidatorUtils.validateEntity(hiveConfConnectVO);
                BeanUtils.copyProperties(hiveConfConnectVO, confConnect1);
                break;
            case 12:
                // es 连接
                EsConfConnectVO esConfConnectVO = new EsConfConnectVO();
                BeanUtils.copyProperties(confConnect, esConfConnectVO);
                ValidatorUtils.validateEntity(esConfConnectVO);
                BeanUtils.copyProperties(esConfConnectVO, confConnect1);
                break;
            case 13:
                // hbase 连接
                HBaseConfConnectVO hBaseConfConnectVO = new HBaseConfConnectVO();
                BeanUtils.copyProperties(confConnect, hBaseConfConnectVO);
                ValidatorUtils.validateEntity(hBaseConfConnectVO);
                BeanUtils.copyProperties(hBaseConfConnectVO, confConnect1);
                break;
            case 14:
                // kudu 连接
                KuduConfConnectVO kuduConfConnectVO = new KuduConfConnectVO();
                BeanUtils.copyProperties(confConnect, kuduConfConnectVO);
                ValidatorUtils.validateEntity(kuduConfConnectVO);
                BeanUtils.copyProperties(kuduConfConnectVO, confConnect1);
                break;
        }
        return confConnect1;
    }

    /**
     * 前端密码传入的是密文就需要解密
     *
     * @param confConnect 参数
     */
    private String decryptPwd(ConfConnect confConnect) {
        if (StringUtils.isNotEmpty(confConnect.getPassword())) {
            String password = confConnect.getPassword();
            log.info("解密前的密文={}", password);
            if (password.length() >= 32) {//前端传的是密文
                String pwd = AES.decrypt(password,
                        systemCenterConfig.getPublicKey());
                log.info("publicKey={},解密前的密文={},解密后的密文:={}", systemCenterConfig.getPublicKey(), password, pwd);
                if (null == pwd) {
                    return "前后端秘钥加密解密不对称";
                }
                confConnect.setPassword(pwd);
            }
        }
        return null;
    }

    /**
     * 保存配置连接
     */
    @ApiOperation("保存配置连接")
    @SysLog(value = "新增数据源", type = OperationTypeEnum.ADDCODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @PostMapping("/save")
    //todo 0925
    //@RequiresPermissions("bdp:confConnect:save")
    public R save(@RequestBody ConfConnect confConnect) {
        String msg = decryptPwd(confConnect);
        if (StringUtils.isNotEmpty(msg)) {
            return R.error(msg);
        }
        //角色是否存在
        if (null == confConnect.getRoleId()) {
            return R.error("初始化角色必须传递！");
        }
        confConnect = checkConnect(confConnect);
        ValidatorUtils.validateEntity(confConnect);

        BaseRes baseRes = checkJIEkou(confConnect);
        if (baseRes.getCode() == 500) {
            return R.error(baseRes.getMsg());
        }
        if (confConnectService.checkExistByName(confConnect.getName(), null, confConnect)) {
            throw new RRException(DataDevelopmentBizExceptionEnum.CONF_CONNECT_EXISTED.getMessage());
        }
        BaseRes r = confConnectService.testConnect(confConnect, false);
        if (null != r && 200 != r.getCode() && r.getCode() != 0 && r.getCode() != 20000) {
            return R.error("数据源连接失败" + r.getMsg());
        }
        SapConnectField sapConnectField = confConnect.getSapConnectFieldInfo();
        if (sapConnectField != null) {
            SapCheckUtils sapCheckUtils = new SapCheckUtils();
            sapConnectField = sapCheckUtils.checkSapParams(sapConnectField);
        }
        if (!confConnect.getTypeId().equals(ConnectTypeEnum.JieKou.getCode())) {
            try {
                if (StringUtils.isNotBlank(confConnect.getPassword())) {
                    confConnect.setPassword(securityUtil.encrypt(confConnect.getPassword()));
                }
            } catch (Exception e) {
                log.warn("加密失败：", e);
            }
        }
        Integer userId = ShiroUtils.getUserId().intValue();
        confConnect.setCreatePer(userId);
        confConnect.setCreateTime(DateTime.now());
        confConnect.setConnectStatus(1);//1:正常 2:失败
        confConnect.setConnectEnable(1);//0:不启用 1:启用
        confConnect.setTenantId(ShiroUtils.getTenantId());//租户id
        //对应的部门的类型
        if (StringUtils.isNotEmpty(confConnect.getDepartmentId())) {
            Integer departmentType = iDepartmentService.getDepartmentType(confConnect.getDepartmentId());
            confConnect.setDepartmentType(departmentType);
        }
        try {
            //授权的角色id
            Long roleId = confConnect.getRoleId();

            boolean re = confConnectService.save(confConnect);
            int confConnectId = confConnect.getId();
            if (re) {
                ConfConnect confConnectOne = confConnectService.getOne(new QueryWrapper<ConfConnect>().eq("name", confConnect.getName())
                        .eq("create_per", ShiroUtils.getUserId().intValue()));
                if (confConnectOne == null) {
                    return R.error();
                } else {
                    //kafka
                    if (confConnectOne.getTypeId().equals(ConnectTypeEnum.Sap.getCode())) {
                        if (null == sapConnectField) {
                            return R.error(DataDevelopmentBizExceptionEnum.CONF_CONNECT_FIELD_NULL.getMessage());
                        }
                        SapConnectFieldDb sapConnectFieldDb = new SapConnectFieldDb();
                        BeanUtils.copyProperties(sapConnectField, sapConnectFieldDb);
                        if (CollectionUtil.isNotEmpty(sapConnectField.getAinputParams())) {
                            sapConnectFieldDb.setAinputParams(sapConnectField.getAinputParams().toString());
                        }
                        if (CollectionUtil.isNotEmpty(sapConnectField.getAoutputParams())) {
                            sapConnectFieldDb.setAoutputParams(sapConnectField.getAoutputParams().toString());
                        }
                        sapConnectFieldDb.setConnectId(confConnectOne.getId());
                        sapConnectFieldService.save(sapConnectFieldDb);
                    }
                    //接口
                    else if (confConnectOne.getTypeId().equals(ConnectTypeEnum.JieKou.getCode())) {
                        List<ConnectFieldInfo> list = confConnect.getConnectFieldInfos();
                        /**动态token*/
                        if (confConnect.getTokenUrl() != null) {
                            ConnectFieldInfo connectFieldInfo = new ConnectFieldInfo();
                            connectFieldInfo.setConnectId(confConnectOne.getId());
                            connectFieldInfo.setFieldType(3);
                            connectFieldInfo.setFieldName(confConnect.getVariableName());
                            connectFieldInfo.setFieldValue(confConnect.getTokenUrl());
                            connectFieldInfo.setFieldAlias("$token^");
                            list.add(connectFieldInfo);
                        }
                        /**动态引用*/
                        if (confConnect.getVariableValue() != null) {
                            ConnectFieldInfo connectFieldInfo = new ConnectFieldInfo();
                            connectFieldInfo.setConnectId(confConnectOne.getId());
                            connectFieldInfo.setFieldType(3);
                            connectFieldInfo.setFieldName(confConnect.getVariableName());
                            connectFieldInfo.setFieldValue(confConnect.getVariableValue());
                            connectFieldInfo.setFieldAlias("$variable^");
                            list.add(connectFieldInfo);
                        }
                        connectFieldInfoService.saveConnectFieldInfos(list, confConnectOne.getId());
                    } else if (confConnectOne.getTypeId().equals(ConnectTypeEnum.WebService.getCode())) {
                        List<ConnectFieldInfo> list = confConnect.getConnectFieldInfos();
                        connectFieldInfoService.saveConnectFieldInfos(list, confConnectOne.getId());
                    }
                    //保存成功，并且给对应的数据源授权初始化角色信息
                    dataPermissionService.initDataPermission(confConnectId, 1, ShiroUtils.getUserId().intValue(), roleId);
                }
            }
        } catch (Exception ex) {
            throw new RRException(DataDevelopmentBizExceptionEnum.SERVER_ERROR.getMessage());
        }
        return R.ok();
    }

    @Resource
    private RestHighLevelClient client;

    @Value("${elasticsearch.dataSourceIndexName}")
    private String dataSourceIndexName;
    /**
     * 删除配置连接
     */
    @ApiOperation("删除配置连接")
    @SysLog(value = "删除数据源", type = OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @PostMapping("/delete")
    //todo 0925
    //@RequiresPermissions("bdp:confConnect:delete")
    public R delete(@ApiParam(value = "{'id':1}") @RequestBody ConfConnect param) {
        if (!confConnectService.checkExistById(param.getId())) {
            throw new RRException(DataDevelopmentBizExceptionEnum.CONF_CONNECT_NOT_EXIST.getMessage());
        }
        if (confConnectService.checkIfRef(param.getId())) {
            throw new RRException("当前数据源已被项目关联，不可删除");
        }
        //验证是否有启用的任务，在使用这个连接。如果有，则不能删除
        confConnectService.checkAnalysisInfoUsed(param.getId(), 1);

        if (!confConnectService.checkPermission(param.getId()) && //不是自己创建的数据 才做权限校验
                !dataPermissionService.checkDataPermission(param.getId(),
                        PermissionTypeEnum.RESOURCE.getType(), PermissionTypeEnum.DELETE.getType())) {
            throw new RRException(DataDevelopmentBizExceptionEnum.DELETE_NO_PERMISSION.getMessage());
        }
        try {
            confConnectService.removeById(param.getId());
            connectFieldInfoService.remove(new QueryWrapper<ConnectFieldInfo>().eq("connect_id", param.getId()));
        } catch (Exception ex) {
            throw new RRException(DataDevelopmentBizExceptionEnum.SERVER_ERROR.getMessage());
        }
        //删除es中的数据源
        BulkRequest request = new BulkRequest();
        if (param.getId()!=null) {
            request.add(new DeleteRequest().index(dataSourceIndexName).id(param.getId().toString()));
            BulkResponse delete = null;
            try {
                delete = this.client.bulk(request, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return R.ok();
    }

    /**
     * 修改配置连接
     */
    @ApiOperation("修改配置连接")
    @SysLog(value = "修改数据源", type = OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @PostMapping("/update")
    //todo 0925
    //@RequiresPermissions({"bdp:confConnect:update","bdp:procInfo:config"})
    public R update(@RequestBody ConfConnect param) {
        ValidatorUtils.validateEntity(param);
        if (param.getProcId() != null && param.getProcId() != 0) {
            if (!procUserService.checkViewPermission(param.getProcId())) {
                throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_PERMISSIOIN.getMessage());
            }
        }
//        String msg = decryptPwd(param);
//        if (StringUtils.isNotEmpty(msg)) {
//            return R.error(msg);
//        }
        BaseRes r = testConnect(param);
        if (null == r || r.getCode() != 0) {
            return R.error(r.getMsg());
        }
        if (confConnectService.checkExistByName(param.getName(), param.getId(), param)) {
            throw new RRException(DataDevelopmentBizExceptionEnum.CONF_CONNECT_EXISTED.getMessage());
        }
        if (!confConnectService.checkPermission(param.getId()) && //不是自己创建的数据 才做权限校验
                !dataPermissionService.checkDataPermission(param.getId(),
                        PermissionTypeEnum.RESOURCE.getType(), PermissionTypeEnum.UPDATE.getType())) {
            throw new RRException(DataDevelopmentBizExceptionEnum.UPDATE_NO_PERMISSION.getMessage());
        }
        //创建人和创建时间
        ConfConnect olderConnect = confConnectService.getById(param.getId());
        param.setCreatePer(olderConnect.getCreatePer());
        param.setCreateTime(olderConnect.getCreateTime());
        //租户id
        param.setTenantId(olderConnect.getTenantId());

        ConfConnect confConnect = confConnectService.getOne(new QueryWrapper<ConfConnect>().eq("name", param.getName()).eq("create_per", param.getCreatePer()));
        if (confConnect != null && confConnect.getId().intValue() != param.getId().intValue()) {
            throw new RRException(DataDevelopmentBizExceptionEnum.CONF_CONNECT_EXISTED.getMessage());
        }
        //验证分析任务是否有正在运行
        //confConnectService.checkAnalysisInfoUsed(confConnect.getId(),2);
        //判断是否时密文
        if (null != param.getPassword() && !"".equals(param.getPassword())) {
            try {
                param.setPassword(securityUtil.encrypt(param.getPassword()));
            } catch (Exception e) {
                throw new RRException("加密失败");
            }
        }
        param.setModPer(ShiroUtils.getUserId().intValue());
        param.setModTime(DateTime.now());
        param.setConnectStatus(1);//1:正常 2:失败
        param.setConnectEnable(1);//0:不启用 1:启用
        if (StringUtils.isEmpty(param.getDbname())) {
            param.setDbname("");
        }
        //hive类型的数据源类型的字段
        if (StringUtils.isEmpty(param.getCronQueue())) {
            param.setCronQueue("");
        }
        if (StringUtils.isEmpty(param.getDefaultFs())) {
            param.setDefaultFs("");
        }
        if (StringUtils.isEmpty(param.getMetastoreUris())) {
            param.setMetastoreUris("");
        }
        //对应的部门的类型
        if (StringUtils.isNotEmpty(param.getDepartmentId())) {
            Integer departmentType = iDepartmentService.getDepartmentType(param.getDepartmentId());
            param.setDepartmentType(departmentType);
        } else {
            param.setDepartmentId("");
            param.setDepartmentType(null);
        }

        try {
            Boolean re = confConnectService.updateById(param);
            if (re) {
                ConfConnect confConnectOne = confConnectService.getById(param.getId());
                if (confConnectOne == null) {
                    return R.error();
                } else {
                    if (confConnectOne.getTypeId() == ConnectTypeEnum.JieKou.getCode()) {
                        List<ConnectFieldInfo> list = param.getConnectFieldInfos();
                        /**判断是动态秘钥还是动态变量*/
                        if (param.getTokenUrl() != null) {
                            ConnectFieldInfo connectFieldInfo = new ConnectFieldInfo();
                            connectFieldInfo.setConnectId(param.getId());
                            connectFieldInfo.setFieldType(3);
                            connectFieldInfo.setFieldName(param.getVariableName());
                            connectFieldInfo.setFieldValue(param.getTokenUrl());
                            connectFieldInfo.setFieldAlias("$token^");
                            list.add(connectFieldInfo);
                        }
                        if (param.getVariableValue() != null) {
                            ConnectFieldInfo connectFieldInfo = new ConnectFieldInfo();
                            connectFieldInfo.setConnectId(param.getId());
                            connectFieldInfo.setFieldType(3);
                            connectFieldInfo.setFieldName(param.getVariableName());
                            connectFieldInfo.setFieldValue(param.getVariableValue());
                            connectFieldInfo.setFieldAlias("$variable^");
                            list.add(connectFieldInfo);
                        }
                        /**删除原来的字段*/
                        connectFieldInfoService.remove(new QueryWrapper<ConnectFieldInfo>().eq("connect_id", confConnectOne.getId()));
                        connectFieldInfoService.saveConnectFieldInfos(list, confConnectOne.getId());
                        return R.ok();
                    }
                    //更新sap数据源配置
                    else if (confConnectOne.getTypeId().equals(ConnectTypeEnum.Sap.getCode())) {
                        SapConnectField sapConnectField = param.getSapConnectFieldInfo();
                        SapConnectFieldDb sapConnectFieldDb = new SapConnectFieldDb();
                        if (sapConnectField == null) {
                            return R.error(DataDevelopmentBizExceptionEnum.CONF_CONNECT_FIELD_NULL.getMessage());
                        }
                        BeanUtils.copyProperties(sapConnectField, sapConnectFieldDb);
                        if (CollectionUtil.isNotEmpty(sapConnectField.getAinputParams())) {
                            sapConnectFieldDb.setAinputParams(sapConnectField.getAinputParams().toString());
                        }
                        if (CollectionUtil.isNotEmpty(sapConnectField.getAoutputParams())) {
                            sapConnectFieldDb.setAoutputParams(sapConnectField.getAoutputParams().toString());
                        }
                        sapConnectFieldDb.setConnectId(confConnectOne.getId());
                        // 删除原来的字段
                        // SapConnectFieldDb sapConnectFieldDb = sapConnectFieldService.getOne()
                        sapConnectFieldService.updateById(sapConnectFieldDb);
                        return R.ok();
                    } else if (confConnectOne.getTypeId().equals(ConnectTypeEnum.WebService.getCode())) {
                        List<ConnectFieldInfo> list = param.getConnectFieldInfos();
                        //删除原来的字段
                        connectFieldInfoService.remove(new QueryWrapper<ConnectFieldInfo>().eq("connect_id", confConnectOne.getId()));
                        connectFieldInfoService.saveConnectFieldInfos(list, confConnectOne.getId());
                        //长静补充需求：如果是jdbc的数据源设置了数据库，则找到引入了的采集任务，把采集任务的数据库保持和源的数据库与一致
                    } else if (confConnectOne.getCode().equals(ConTypeType.JDBC.getCode())) {
                        Integer sourceId = confConnectOne.getId();
                        String dbName = confConnectOne.getDbname();
                        if (sourceId != null && sourceId > 0 && dbName != null && StringUtils.isNotBlank(dbName)) {
                            confConnectService.updateCollectionTaskDbName(sourceId, dbName);
                        }

                    }
                }
            }
        } catch (Exception ex) {
            throw new RRException(DataDevelopmentBizExceptionEnum.SERVER_ERROR.getMessage());
        }
        return R.ok();
    }


    @ApiOperation("启用或者禁用")
    @SysLog(value = "禁用/启用数据源", type = OperationTypeEnum.OPENCLOSECODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/enableOrDisable/{id}")
    public BaseRes enableOrDisable(@PathVariable("id") Long id) {
        if (!confConnectService.checkPermission(id.intValue()) && //不是自己创建的数据 才做权限校验
                !dataPermissionService.checkDataPermission(id.intValue(),
                        PermissionTypeEnum.RESOURCE.getType(), PermissionTypeEnum.UPDATE.getType())) {
            throw new RRException(DataDevelopmentBizExceptionEnum.UPDATE_NO_PERMISSION.getMessage());
        }
        confConnectService.enableOrDisable(id);
        return BaseRes.ok();
    }

    /**
     * 配置连接详情
     */
    @ApiOperation("配置连接详情")
    @SysLog(value = "配置连接详情", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/info/{id}")
    //todo 0925
    //@RequiresPermissions({"bdp:confConnect:list"})
    public R info(@PathVariable("id") Long id) {
        ConfConnectDetail confConnect = confConnectService.getDetail(id);
        if (confConnect == null) {
            return R.error("数据不存在！");
        }
        List<ConnectFieldInfo> connectFieldInfos = connectFieldInfoService.list(new QueryWrapper<ConnectFieldInfo>().eq("connect_id", confConnect.getId()));
        confConnect.setConnectFieldInfos(connectFieldInfos);
        if (confConnect.getTypeId().equals(ConnectTypeEnum.Sap.getCode())) {
            SapConnectFieldDb sapConnectFieldDb = sapConnectFieldService.getOne(new QueryWrapper<SapConnectFieldDb>().eq("connect_id", confConnect.getId()));
            if (sapConnectFieldDb != null) {
                String inputParams = sapConnectFieldDb.getAinputParams();
                String outputParams = sapConnectFieldDb.getAoutputParams();
                SapConnectField sapConnectField = new SapConnectField();
                BeanUtils.copyProperties(sapConnectFieldDb, sapConnectField);
                sapConnectField.setAinputParams(JSONObject.parseArray(inputParams, JSONObject.class));
                sapConnectField.setAoutputParams(JSONObject.parseArray(outputParams, JSONObject.class));
                confConnect.setSapConnectFieldInfo(sapConnectField);
            } else {
                SapConnectField sapConnectField = new SapConnectField();
                generateNullSap(sapConnectField);
                confConnect.setSapConnectFieldInfo(sapConnectField);
            }
        } else {
            SapConnectField sapConnectField = new SapConnectField();
            generateNullSap(sapConnectField);
            confConnect.setSapConnectFieldInfo(sapConnectField);
        }
        /**接口隐藏不需要的数据*/
        if (confConnect.getTypeId().equals(ConnectTypeEnum.JieKou.getCode()) || confConnect.getTypeId().equals(ConnectTypeEnum.WebService.getCode())) {
            List<ConnectFieldInfo> connectFieldInfos1 = connectFieldInfoService.list(new QueryWrapper<ConnectFieldInfo>().eq("connect_id", confConnect.getId()));
            /**隐藏*/
            if (connectFieldInfos1 != null && connectFieldInfos1.size() > 0) {
                ListIterator<ConnectFieldInfo> iter = connectFieldInfos1.listIterator();
                while (iter.hasNext()) {
                    ConnectFieldInfo next = iter.next();
                    if ("$token^".equalsIgnoreCase(next.getFieldAlias())) {
                        iter.remove();
                    }
                    if ("$variable^".equalsIgnoreCase(next.getFieldAlias())) {
                        iter.remove();
                    }
                }
            }
            confConnect.setConnectFieldInfos(connectFieldInfos1);
        }
        //模糊匹配目录结构的部门信息数据
        confConnect.setTypeName(ConnectTypeEnum.ObjOf(confConnect.getTypeId()).getName());
        return R.okWithData(confConnect);
    }

    //生成空的SAP数据
    private void generateNullSap(SapConnectField sapConnectField) {
        List<JSONObject> inputParams = new ArrayList<>();
        List<JSONObject> outputParams = new ArrayList<>();
        JSONObject inputParam = new JSONObject();
        JSONObject outputParam = new JSONObject();
        String[] sapParams = {"fieldName", "fieldKind", "fieldValue", "fieldAlias", "fieldInputType", "fieldDataType"};
        for (String s : sapParams) {
            inputParam.put(s, "");
            outputParam.put(s, "");
        }
        inputParams.add(inputParam);
        outputParams.add(outputParam);
        sapConnectField.setAinputParams(inputParams);
        sapConnectField.setAoutputParams(outputParams);
    }

    /**
     * 获取某类型的所有的配置连接
     */
    @ApiOperation("获取某类型的所有的配置连接")
    //@SysLog(value="获取某类型的所有的配置连接",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/listByType/{type}")
//    @RequiresPermissions("bdp:confConnect:listByType")
    public R listByType(@PathVariable("type") Long type) {
        List<ConfConnect> list = confConnectService.list(new QueryWrapper<ConfConnect>().eq("type_id", type)
                .eq("create_per", ShiroUtils.getUserId().intValue()));
        return R.okWithData(list);
    }


    /**
     * 获取所有的配置类别
     */
    @ApiOperation("1全部显示,2代表输入数据源,3代表输出数据源")
    //@SysLog(value="获取所有的配置类别",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/listTypes")
    //todo 0925
    //@RequiresPermissions({"bdp:confConnect:list"})
    public R listTypes(@ApiParam("{'requstId':'1'}") @RequestParam(value = "requstId") Integer requstId) {
        List<JSONObject> data = new ArrayList<>();
        if (requstId == null) {
            throw new RRException(DataDevelopmentBizExceptionEnum.PARAM_ERROR.getMessage());
        }

        switch (requstId) {
            case 1:
                for (ConnectTypeEnum e : ConnectTypeEnum.values()) {
                    if (e.getEnable()) {
                        JSONObject obj = new JSONObject();
                        obj.put("id", e.getCode());
                        obj.put("name", e.getName());
                        obj.put("type", e.getType().getCode());
                        data.add(obj);
                    }
                }
                break;
            case 2:
                for (ConnectTypeEnum e : ConnectTypeEnum.values()) {
                    if (e.getIsInput()) {
                        JSONObject obj = new JSONObject();
                        obj.put("id", e.getCode());
                        obj.put("name", e.getName());
                        obj.put("type", e.getType().getCode());
                        data.add(obj);
                    }
                }
                break;
            case 3:
                for (ConnectTypeEnum e : ConnectTypeEnum.values()) {
                    if (e.getIsOutput()) {
                        JSONObject obj = new JSONObject();
                        obj.put("id", e.getCode());
                        obj.put("name", e.getName());
                        obj.put("type", e.getType().getCode());
                        data.add(obj);
                    }
                }
                break;
        }

        return R.okWithData(data);
    }

    /**
     * 获取对应任务的存储引擎
     */
    @ApiOperation("1显示数据同步显示,2输出任务显示,3代表sql任务显示")
    //@SysLog(value="获取对应任务的存储引擎",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/listSorageEngine")
    public R listSorageEngine(Integer requstId) {
        List<JSONObject> data = new ArrayList<>();
        if (requstId == null) {
            throw new RRException(DataDevelopmentBizExceptionEnum.PARAM_ERROR.getMessage());
        }
        if (requstId == 1) {
            for (StorageEngineEnum e : StorageEngineEnum.values()) {
                if (e.isDataAsync()) {
                    JSONObject obj = new JSONObject();
                    obj.put("id", e.getCode());
                    obj.put("name", e.getName());
                    data.add(obj);
                }
            }
        }
        if (requstId == 2) {
            for (StorageEngineEnum e : StorageEngineEnum.values()) {
                if (e.isOutputTask()) {
                    JSONObject obj = new JSONObject();
                    obj.put("id", e.getCode());
                    obj.put("name", e.getName());
                    data.add(obj);
                }
            }
        }
        if (requstId == 3) {
            for (StorageEngineEnum e : StorageEngineEnum.values()) {
                if (e.isSqlTask()) {
                    JSONObject obj = new JSONObject();
                    obj.put("id", e.getCode());
                    obj.put("name", e.getName());
                    data.add(obj);
                }
            }
        }
        return R.okWithData(data);

    }

    /**
     * 获取对应任务的查询引擎
     */
    @ApiOperation("1显示数据同步显示,2输出任务显示,3代表sql任务显示")
    //@SysLog(value="获取对应任务的查询引擎",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/listQueryEngine")
    public R listQueryEngine(Integer requstId) {
        List<JSONObject> data = new ArrayList<>();
        if (requstId == null) {
            throw new RRException(DataDevelopmentBizExceptionEnum.PARAM_ERROR.getMessage());
        }
        if (requstId == 1) {
            for (QueryEngineeEnum e : QueryEngineeEnum.values()) {
                if (e.isDataAsync()) {
                    JSONObject obj = new JSONObject();
                    obj.put("id", e.getCode());
                    obj.put("name", e.getName());
                    data.add(obj);
                }
            }
        }
        if (requstId == 2) {
            for (QueryEngineeEnum e : QueryEngineeEnum.values()) {
                if (e.isOutputTask()) {
                    JSONObject obj = new JSONObject();
                    obj.put("id", e.getCode());
                    obj.put("name", e.getName());
                    data.add(obj);
                }
            }
        }
        if (requstId == 3) {
            for (StorageEngineEnum e : StorageEngineEnum.values()) {
                if (e.isSqlTask()) {
                    JSONObject obj = new JSONObject();
                    obj.put("id", e.getCode());
                    obj.put("name", e.getName());
                    data.add(obj);
                }
            }
        }
        return R.okWithData(data);

    }

    /**
     * 获取所有的配置类别
     */
    @ApiOperation("获取编码的所有类型")
    //@SysLog(value="获取编码的所有类型",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/listCodeTypes")
    //todo 0925
    //@RequiresPermissions({"bdp:confConnect:list"})
    public R listCodeTypes() {
        List<JSONObject> data = new ArrayList<>();
        for (CodeType e : CodeType.values()) {
            JSONObject obj = new JSONObject();
            obj.put("id", e.getId());
            obj.put("name", e.getName());
            data.add(obj);
        }
        return R.okWithData(data);
    }

    /**
     * 获取sap数据源数据类型
     */
    @ApiOperation("获取sap数据源数据类型")
    //@SysLog(value="获取sap数据源数据类型",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/listSapDataTypes")
    //todo 0925
    //@RequiresPermissions({"bdp:confConnect:list"})
    public R listSapDataTypes() {
        List<JSONObject> sapDataTypes = new ArrayList<>();
        for (SapDataType e : SapDataType.values()) {
            JSONObject obj = new JSONObject();
            obj.put("id", e.getId());
            obj.put("name", e.getName());
            sapDataTypes.add(obj);
        }
        return R.okWithData(sapDataTypes);
    }

    /**
     * 项目关联数据源
     */
    @ApiOperation("项目关联数据源")
    @SysLog(value = "项目关联数据源", type = OperationTypeEnum.RELATIONCODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @PostMapping("/procRelation")
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:config"})
    public R procRelation(@ApiParam(value = "{'procId':1,'connectId':[1,1]}") @RequestBody Map<String, Object> params) {
        if (!checkRelation(params)) {
            return R.error("请选择需要关联的数据源");
        }
        Integer procId = Integer.parseInt(params.get("procId").toString());
        if (!procUserService.checkViewPermission(procId)) {
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_PERMISSIOIN.getMessage());
        }
        List<Integer> connectIdList = (List<Integer>) params.get("connectId");

        for (Integer connectId : connectIdList) {
            if (procConnectService.checkIfRelation(procId, connectId)) {
                return R.error("有部分数据源已关联");
            } else {
                try {
                    ProcConnect procConnect = new ProcConnect();
                    procConnect.setProcId(procId);
                    procConnect.setConnectId(connectId);
                    procConnectService.save(procConnect);

                } catch (Exception ex) {
                    throw new RRException(DataDevelopmentBizExceptionEnum.SERVER_ERROR.getMessage());
                }
            }
        }
        return R.ok();
    }

    /**
     * 项目取消关联数据源
     */
    @ApiOperation("项目取消关联数据源")
    @SysLog(value = "项目取消关联数据源", type = OperationTypeEnum.DISASSOCIATIONCODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @PostMapping("/procUnRelation")
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:config"})
    public R procUnRelation(@ApiParam(value = "{'procId':1,'connectId':1}")
                            @RequestBody Map<String, Object> params) {
        if (!checkRelation(params)) {
            return R.error("请选择需要取消关联的数据源");
        }
        Integer procId = Integer.parseInt(params.get("procId").toString());
        Integer connectId = Integer.parseInt(params.get("connectId").toString());
        if (!procConnectService.checkTaskRelation(procId, connectId)) {
            return R.error("当前数据源已被任务引用，不可取消关联");
        }
        if (!procUserService.checkViewPermission(procId)) {
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_PERMISSIOIN.getMessage());
        }
        if (!procConnectService.checkIfRelation(procId, connectId)) {
            return R.error("未关联");
        } else {
            procConnectService.remove(new QueryWrapper<ProcConnect>().eq("proc_id", procId).eq("connect_id", connectId));
        }
        return R.ok();
    }

    private boolean checkRelation(Map<String, Object> params) {
        return params.size() == 2
                && params.containsKey("procId")
                && params.containsKey("connectId");
    }

    /**
     * 测试连接是否可用
     */
    @ApiOperation("测试连接是否可用")
    @PostMapping("/testConnect")
    @SysLog(value = "测试连接是否可用", type = OperationTypeEnum.CHECKCODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions("bdp:confConnect:update")
    public BaseRes testConnect(@RequestBody(required = false) ConfConnect confConnect) {
        String msg = decryptPwd(confConnect);
        if (StringUtils.isNotEmpty(msg)) {
            return BaseRes.error(msg);
        }
        confConnect = checkConnect(confConnect);
        BaseRes baseRes = checkJIEkou(confConnect);
        if (baseRes.getCode() == 500) {
            return baseRes;
        }
        BaseRes res = confConnectService.testConnect(confConnect, false);
        return res;
    }


    /**
     * 测试连接是否可用
     */
    @ApiOperation("测试连接是否可用")
    @PostMapping("/getEsIndexList")
    @SysLog(value = "测试连接是否可用", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    public R getEsIndexList(@RequestBody(required = false) ConfConnect confConnect) {
        EsDto esDto = new EsDto();
        List<String> indexList = new ArrayList<>();
        try {
            BeanUtil.copyProperties(confConnect, esDto);
            esDto.setIp(confConnect.getHost());
            indexList = esService.getIndexList(esDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.okWithData(indexList);
    }

//    /**
//     * 获取SAP数据源信息
//     */
//    @ApiOperation("sap数据源详情")
//    @SysLog(value="获取列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.DATA_EXPLORE_MODULE_CODE)
//    @GetMapping("/paramsInfo/{confConnectId}/{jobNodeId}")
//    public R paramsInfo(@PathVariable("confConnectId") String confConnectId, @PathVariable("jobNodeId") Integer jobNodeId) {
//
//        return R.ok();
//    }

    /**
     * 获取SAP数据源信息
     */
    @ApiOperation("sap数据源详情")
    @SysLog(value = "sap数据源详情", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.DATA_SOURCE_MANAGER_MODULE_CODE)
    @GetMapping("/sapInfo/{confConnectId}/{jobNodeId}")
    public R SapInfo(@PathVariable("confConnectId") String confConnectId, @PathVariable("jobNodeId") Integer jobNodeId) {
        ConfConnect confConnect = confConnectService.getById(confConnectId);
        if (null == confConnect) {
            throw new RRException("该数据源不存在请重新配置");
        }
        JobNodeInfo jobNodeInfo = jobNodeInfoService.getById(jobNodeId);
        JobNodeConfigVo jobNodeConfigVo = new JobNodeConfigVo();
        SapConnectField jobConfigSapConnectField = new SapConnectField();
        Integer olderConnectId = null;
        if (jobNodeInfo != null) {
            List<JobNodeConf> jobNodeConfs = jobNodeConfService.list(new QueryWrapper<JobNodeConf>()
                    .eq("job_node_id", jobNodeInfo.getId())
                    .eq("job_type", 0));
            JobNodeConfigDto jobNodeConfigDto = jobNodeConfService.getJobNodeConf(jobNodeConfs);
            if (null != jobNodeConfigDto) {
                olderConnectId = jobNodeConfigDto.getInput_connect_id();
                //获取SAP数据源配置（和最开始的数据源不同，是单独保存的配置信息）
                String sapConnectFieldInfos = jobNodeConfigDto.getSapConnectFieldInfos();
                BeanUtils.copyProperties(jobNodeConfigDto, jobNodeConfigVo);
                if (sapConnectFieldInfos != null) {
                    JSONObject jsonObject = JSONObject.parseObject(sapConnectFieldInfos);
//              MapToPojo<SapConnectField> sapConnectFieldMapToPojo = new MapToPojo<SapConnectField>(jobConfigSapConnectField);
                    jobConfigSapConnectField = mapToSapConnectField(jsonObject);
                }
            }
        }

        SapConnectField sapConnectField = new SapConnectField();
        List<JSONObject> inputParamsList = new ArrayList<>();
        List<JSONObject> outputParamsList = new ArrayList<>();
        if (ConnectTypeEnum.Sap.getCode().equals(confConnect.getTypeId())) {

            //设置多表多行参数
            sapConnectField.setAinputMoreTableParams(jobConfigSapConnectField.getAinputMoreTableParams());
            sapConnectField.setParamType(jobConfigSapConnectField.getParamType());

            SapConnectFieldDb sapConnectFieldDb = sapConnectFieldService.getOne(new QueryWrapper<SapConnectFieldDb>().eq("connect_id", confConnectId));
            if (sapConnectFieldDb != null) {
                BeanUtils.copyProperties(sapConnectFieldDb, sapConnectField);
                String inputParams = sapConnectFieldDb.getAinputParams();
                String outputParams = sapConnectFieldDb.getAoutputParams();
                JSONArray inputJsonArray = JSONArray.parseArray(inputParams);
                if (null != inputJsonArray) {
                    for (int i = 0; i < inputJsonArray.size(); i++) {
                        inputParamsList.add(JSONObject.parseObject(JSONArray.parseArray(inputParams).get(i).toString()));
                    }
                }
                JSONArray outputJsonArray = JSONArray.parseArray(outputParams);
                if (null != outputJsonArray) {
                    for (int i = 0; i < outputJsonArray.size(); i++) {
                        outputParamsList.add(JSONObject.parseObject(JSONArray.parseArray(outputParams).get(i).toString()));
                    }
                }
            }
        }
        /*** WebService */
        else if (ConnectTypeEnum.WebService.getCode().equals(confConnect.getTypeId())) {
            if (Objects.equals(confConnect.getRequestType(), 3)) { //raw
                if (Objects.equals(confConnect.getId(), olderConnectId)) {
                    sapConnectField.setReqType(jobNodeConfigVo.getReqType());
                    sapConnectField.setWebserviceText(jobNodeConfigVo.getWebserviceText());
                    sapConnectField.setResType(jobNodeConfigVo.getResType());
                    sapConnectField.setAnaTableNameUrl(jobNodeConfigVo.getAnaTableNameUrl());
                }
                sapConnectField.setOlderRequestType(confConnect.getRequestType());
            } else {
                Integer olderRequestType = jobNodeConfigVo.getOlderRequestType();
                if (Objects.equals(confConnect.getId(), olderConnectId)) {
                    if (null == olderRequestType) {
                        sapConnectField.setOlderRequestType(confConnect.getRequestType());
                    } else {
                        sapConnectField.setOlderRequestType(olderRequestType);
                    }
                } else {
                    sapConnectField.setOlderRequestType(confConnect.getRequestType());
                }
                List<ConnectFieldInfo> connectFieldInfos = connectFieldInfoService.list(new QueryWrapper<ConnectFieldInfo>().eq("connect_id", confConnectId));
                if (CollectionUtil.isNotEmpty(connectFieldInfos)) {
                    for (int i = 0; i < connectFieldInfos.size(); i++) {
                        JSONObject jsonObject = JSONObject.parseObject(JSONUtil.toJsonStr(connectFieldInfos.get(i)));
                        inputParamsList.add(jsonObject);
                    }
                }
            }
        }
        /**接口数据源回显*/
        else if (ConnectTypeEnum.JieKou.getCode().equals(confConnect.getTypeId())) {
            ConfConnect connect = confConnectService.getOne(new QueryWrapper<ConfConnect>().eq("id", confConnectId));
            if (connect == null) {
                throw new RRException("未查到该数据源的相关配置数据");
            } else {
                List<ConnectFieldInfo> connectFieldInfos = connectFieldInfoService.list(new QueryWrapper<ConnectFieldInfo>().eq("connect_id", confConnectId));
                /**移除不显示的*/
                if (connectFieldInfos != null && connectFieldInfos.size() > 0) {
                    ListIterator<ConnectFieldInfo> iter = connectFieldInfos.listIterator();
                    while (iter.hasNext()) {
                        ConnectFieldInfo next = iter.next();
                        if ("$token^".equalsIgnoreCase(next.getFieldAlias())) {
                            iter.remove();
                        }
                        if ("$variable^".equalsIgnoreCase(next.getFieldAlias())) {
                            iter.remove();
                        }
                    }
                }
                /***显示出数据源的数据*/
                for (int i = 0; i < connectFieldInfos.size(); i++) {
                    JSONObject jsonObject = JSONObject.parseObject(JSONUtil.toJsonStr(connectFieldInfos.get(i)));
                    inputParamsList.add(jsonObject);
                }
            }
        } else if (ConnectTypeEnum.ElasticSearch.getCode().equals(confConnect.getTypeId())) {
            if (Objects.equals(confConnect.getId(), olderConnectId)) {
                sapConnectField.setEsSqlCondition(jobNodeConfigVo.getEsSqlCondition());
                sapConnectField.setEsIndex(jobNodeConfigVo.getEsIndex());
                sapConnectField.setEsIndexType(jobNodeConfigVo.getEsIndexType());
                sapConnectField.setEsPageSize(jobNodeConfigVo.getEsPageSize());
                sapConnectField.setEsPageTime(jobNodeConfigVo.getEsPageTime());
            }
        }

        //判断是编辑还是新增（否相等对应的数据源连接id）
        if (Objects.equals(confConnect.getId(), olderConnectId)) {
            List<JSONObject> newInputParamsList = new ArrayList<>();
            List<JSONObject> newOutputParamsList = new ArrayList<>();
            if (null != jobConfigSapConnectField) {
                if (CollectionUtil.isNotEmpty(jobConfigSapConnectField.getAinputParams())) {
                    newInputParamsList.addAll(jobConfigSapConnectField.getAinputParams());
                }
                if (CollectionUtil.isNotEmpty(jobConfigSapConnectField.getAoutputParams())) {
                    newOutputParamsList.addAll(jobConfigSapConnectField.getAoutputParams());
                }
            }
            sapConnectField.setAinputParams(newInputParamsList);
            sapConnectField.setAoutputParams(newOutputParamsList);
        } else {
            sapConnectField.setAinputParams(inputParamsList);
            sapConnectField.setAoutputParams(outputParamsList);
        }
//        //校验jobConfig中的input和output的key和数据源中的是否相同
//        if (null != jobConfigSapConnectField && jobConfigSapConnectField.getId() != null) {
//            List<JSONObject> ainputParams = jobConfigSapConnectField.getAinputParams();
//            List<JSONObject> aoutputParams = jobConfigSapConnectField.getAoutputParams();
//            if (jobConfigSapConnectField.getId().equals(sapConnectField.getId())) {
//                if (inputParamsList != null && !ainputParams.equals(inputParamsList)) {
//                    for (JSONObject inputForConnectConf : inputParamsList) {
//                        boolean flag = false;
//                        for (JSONObject inputForNodeConf : ainputParams) {
//                            if (inputForNodeConf.get("fieldName").equals(inputForConnectConf.get("fieldName"))) {
//                                flag = true;
//                                break;
//                            }
//                        }
//                        /**如果配置文件没有输入参数那么就为空*/
////                        if(ainputParams.size()>0) {
//                            if (!flag) {
//                                ainputParams.add(inputForConnectConf);
//                            }
////                        }
//                    }
//                }
//                if (null != outputParamsList && !aoutputParams.equals(outputParamsList)) {
//                    for (JSONObject outputForConnectConf : outputParamsList) {
//                        boolean flag = false;
//                        for (JSONObject outputForNodeConf : aoutputParams) {
//                            if (outputForNodeConf.get("fieldName").equals(outputForConnectConf.get("fieldName"))) {
//                                flag = true;
//                                break;
//                            }
//                        }
//                        if (!flag) {
//                            aoutputParams.add(outputForConnectConf);
//                        }
//                    }
//                }
//                sapConnectField.setAinputParams(ainputParams);
//                sapConnectField.setAoutputParams(aoutputParams);
//            } else {
//                sapConnectField.setAinputParams(inputParamsList);
//                sapConnectField.setAoutputParams(outputParamsList);
//            }
//        } else {
//            sapConnectField.setAinputParams(inputParamsList);
//            sapConnectField.setAoutputParams(outputParamsList);
//        }
        return R.okWithData(sapConnectField);
    }

    public static SapConnectField mapToSapConnectField(Map<String, Object> map) {
        if (map == null)
            return null;
        SapConnectField obj;
        try {
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(map));
            obj = JSON.parseObject(jsonObject.toJSONString(), new TypeReference<SapConnectField>() {
            });
            return obj;
        } catch (Exception e) {
            return null;
        }
    }

    /***
     * 接口参数校验，以及自动设置字段类型
     * @param confConnectOne
     * @return
     */
    public BaseRes checkJIEkou(ConfConnect confConnectOne) {
        BaseRes baseRes = new BaseRes();
        if (confConnectOne.getTypeId().equals(ConnectTypeEnum.JieKou.getCode())) {
            List<ConnectFieldInfo> connectFieldInfos = confConnectOne.getConnectFieldInfos();
            if (connectFieldInfos == null) {
                baseRes.setCode(500);
                baseRes.setMsg("必需带上Token请求参数");
                return baseRes;
            }
        }
        return baseRes;
    }


    /**
     * 测试连接是否可用
     */
    @ApiOperation("测试连接是否可用")
    @PostMapping("/testAllConnect")
    public R testAllConnect(
            @RequestParam(value = "typeId", required = false) Integer typeId
            , @RequestParam(value = "isDebug", required = false, defaultValue = "false") Boolean isDebug) {
        try {

            confConnectService.testAllConnect(typeId, isDebug);
        } catch (Exception e) {
            XxlJobLogger.log(e);
            return R.okWithData(e.getMessage());
        }
        return R.ok();
    }


    //获取关联数据源的创建人列表
    @ApiOperation("获取关联数据源创建人列表")
    @GetMapping("/selectCreaterlist")
    //@SysLog(value="接收人列表",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R selectCreaterlist() {
        List<UserCreaterVO> userCreaterVOS = confConnectService.selectCreaterList();
        JSONArray array = new JSONArray();
        JSONObject object;
        for (UserCreaterVO UserCreater : userCreaterVOS) {
            object = new JSONObject();
            object.put("userId", UserCreater.getCreatePer());
            object.put("userName", UserCreater.getCreateName());
            array.add(object);
        }
        return R.okWithData(array);
    }


//    //获取关联数据源的创建人列表
//    @ApiOperation("根据关联数据源创建人获取信息")
//    @PostMapping("/infoByCreaterlist")
//    public R infoByCreaterlist(@ApiParam(value = "{'name':'test','proc_id':46,'jobId':1,'type_id':1,'searchUserId':318,'page':0,'limit':0}") @RequestBody Map<String, Object> params) {
//        if (null == params.get("proc_id")) {
//            if (null == params.get("jobId")) {
//                throw new RRException(String.format(DataDevelopmentBizExceptionEnum.PARAM_FAIL_ERROR.getMessage(), "工程id或者任务id不能为空"));
//            }
//            //通过job_id查询到对应的job信息
//            JobInfo jobInfo = jobInfoService.getById((Serializable) params.get("jobId"));
//            if (null == jobInfo) {
//                throw new RRException(String.format(DataDevelopmentBizExceptionEnum.PARAM_FAIL_ERROR.getMessage(), "工程id或者任务id不能为空"));
//            }
//            params.put("proc_id", jobInfo.getProcId());
//        }
//        List<ConfConnect> dataSourceList = new ArrayList<>();
//        PageUtils conConnectPage = confConnectService.pageByNameAndProId(params);
//        List<ConfConnect> confConnectList = (List<ConfConnect>) conConnectPage.getList();
//        handleConfConnect(dataSourceList, confConnectList, params);
//        conConnectPage.setList(dataSourceList);
//        return R.okWithPage(conConnectPage);
//        }


}